home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / PROGRAMM / CC_C / 1116.ZIP / TOOLKIT.ARC / STDIO.H < prev    next >
Text File  |  1979-12-31  |  9KB  |  222 lines

  1.  
  2. /* STDIO.H  written by Wayne Pearson for Small C:PC
  3.    valid calls are:
  4.    printf("%d",65); display the decimal integer 65
  5.    printf("%h",65); display the hexadecimal integer 41
  6.    printf("%o",65); display the octal integer 101
  7.    printf("%b",65); display the binary integer 1000001
  8.    printf("\nHello\n\nworld\n",0); display string with embedded line feeds
  9.    prnchar(65);     print 'A' on your printer
  10.    prnstr("Hello"); print the string Hello on your printer
  11.    prnf("%d",65,fptr); prints the decimal integer 65 on your printer
  12.    prnf("%h",65,fptr); prints the hexadecimal integer 41 on your printer
  13.    prnf("%o",65,fptr); prints the octal integer 101 on your printer
  14.    prnf("%b",65,fptr); prints the binary integer 1000001 on your printer
  15.    prnf("\nHello\n\nworld\n",0,fptr); prints string with embedded line feeds
  16. */
  17.  
  18. printf(pf_1,pf_2d)
  19.                    char pf_1[];  int pf_2d;
  20.            {
  21.                   if((pf_1[0] == '%')&(pf_1[1] == 'd')) disp_dec(10,pf_2d);
  22.              else if((pf_1[0] == '%')&(pf_1[1] == 'h')) disp_hex(pf_2d);
  23.              else if((pf_1[0] == '%')&(pf_1[1] == 'o')) disp_dec(8,pf_2d);
  24.              else if((pf_1[0] == '%')&(pf_1[1] == 'b')) disp_bin(pf_2d);
  25.              else disp_str(pf_1);
  26.            }
  27.  
  28. disp_dec(base_n,pf_2)
  29.                        int base_n, pf_2;
  30.      {
  31.        int dec_100, dec_10, dec_1, base_nn;
  32.        base_nn = base_n * base_n;
  33.        if(pf_2 >= base_nn) dec_100 = pf_2 / base_nn; else dec_100 = 0;
  34.        if(pf_2 >= base_n)
  35.                           {
  36.                             dec_10 = pf_2 - dec_100 * base_nn;
  37.                             dec_10 = dec_10 / base_n;
  38.                           }
  39.                       else dec_10 = 0;
  40.        if(pf_2 >= base_nn)
  41.               {
  42.                 dec_1 = pf_2 - dec_100 * base_nn - dec_10 * base_n;
  43.                 if(((dec_100 * base_nn) % base_nn) >= (base_nn/2 -1))--dec_1;
  44.                 if(((dec_10 * base_n) % base_n) >= (base_n / 2 - 1 ))--dec_1;
  45.               }
  46.        if((pf_2 < base_nn)&(pf_2 >= base_n))
  47.               {
  48.                 dec_1 = pf_2 -dec_10 * base_n;
  49.                 if(((dec_10 * base_n) % base_n) >= (base_n / 2 - 1 ))--dec_1;
  50.               }
  51.        if(pf_2 < base_n) dec_1 = pf_2;
  52.        dec_100 = dec_100 + 48; dec_10 = dec_10 + 48;  dec_1 = dec_1 + 48;
  53.        if(dec_100 > 48) putchar(dec_100);
  54.        if(((dec_100 > 48)&(dec_10 >= 48))|(dec_10 > 48)) putchar(dec_10);
  55.        putchar(dec_1);
  56.      }
  57.  
  58. disp_hex(pf_2h)
  59.                 int pf_2h;
  60.         {
  61.           int hex_16, hex_1;
  62.           if(pf_2h >= 16)
  63.                          {
  64.                            hex_16 = pf_2h / 16; hex_1 = pf_2h - hex_16 * 16;
  65.                            if(((hex_16 * 16) % 16) >= 8) --hex_1;
  66.                          }
  67.                     else { hex_16 = 0; hex_1 = pf_2h; }
  68.           if(hex_16 > 9) hex_16 = hex_16 + 7;
  69.           if(hex_1 > 9) hex_1 = hex_1 + 7;
  70.           hex_16 = hex_16 + 48; hex_1 = hex_1 + 48;
  71.           if(hex_16 > 48) putchar(hex_16);  putchar(hex_1);
  72.         }
  73.  
  74. disp_bin(pf_2)
  75.                int pf_2;
  76.           {
  77.             int bin_8,bin_7,bin_6,bin_5,bin_4,bin_3,bin_2,bin_1,bin_mod;
  78.             bin_8 =bin_7 =bin_6 =bin_5 =bin_4 =bin_3 =bin_2 =bin_1 =0;
  79.             bin_mod = pf_2 / 128;  if(bin_mod > 0) bin_8 = 1;
  80.             bin_mod = pf_2 / 64; bin_mod = bin_mod % 2;
  81.             if(bin_mod > 0) bin_7 = 1;
  82.             bin_mod = pf_2 / 32; bin_mod = bin_mod % 2;
  83.             if(bin_mod > 0) bin_6 = 1;
  84.             bin_mod = pf_2 / 16; bin_mod = bin_mod % 2;
  85.             if(bin_mod > 0) bin_5 = 1;
  86.             bin_mod = pf_2 / 8; bin_mod = bin_mod % 2;
  87.             if(bin_mod > 0) bin_4 = 1;
  88.             bin_mod = pf_2 / 4; bin_mod = bin_mod % 2;
  89.             if(bin_mod > 0) bin_3 = 1;
  90.             bin_mod = pf_2 / 2; bin_mod = bin_mod % 2;
  91.             if(bin_mod > 0) bin_2 = 1;
  92.             bin_mod = pf_2 % 2; if(bin_mod > 0) bin_1 = 1;
  93.             if(pf_2 > 255) bin_8 = 40;
  94.             putchar(bin_8 + 48); putchar(bin_7 + 48); putchar(bin_6 + 48);
  95.             putchar(bin_5 + 48); putchar(bin_4 + 48); putchar(bin_3 + 48);
  96.             putchar(bin_2 + 48); putchar(bin_1 + 48);
  97.           }
  98.  
  99. disp_str(str_nm)
  100.                  char str_nm[];
  101.           {
  102.             char chr_str,str_end; int str_lcv; str_end='a'; str_lcv = 0;
  103.             while(str_end != 0)
  104.                   {
  105.                     str_end = str_nm[str_lcv];
  106.                       if(str_end == '\') {++str_lcv; putchar(13);}
  107.                     else {chr_str = str_nm[str_lcv]; putchar(chr_str);}
  108.                     ++str_lcv;
  109.                   }
  110.           }
  111.  
  112. prnstr(str_name)
  113.                   char str_name[];
  114.                 {
  115.                   char str_chr;  int prn_lcv;  prn_lcv = 0;
  116.                   while(str_chr != 0)
  117.                         {
  118.                           str_chr = str_name[prn_lcv];
  119.                           pcdos(5,str_chr);  ++prn_lcv;
  120.                         }
  121.                 }
  122.  
  123. prnchar(chr_name)
  124.                    char chr_name;
  125.                  { pcdos(5,chr_name); return(chr_name); }
  126.  
  127. prnf(f_1,f_2d)
  128.                    char f_1[];  int f_2d;
  129.            {
  130.                   if((f_1[0] == '%')&(f_1[1] == 'd')) prn_dec(10,f_2d);
  131.              else if((f_1[0] == '%')&(f_1[1] == 'h')) prn_hex(f_2d);
  132.              else if((f_1[0] == '%')&(f_1[1] == 'o')) prn_dec(8,f_2d);
  133.              else if((f_1[0] == '%')&(f_1[1] == 'b')) prn_bin(f_2d);
  134.              else prn_str(f_1);
  135.            }
  136.  
  137. prn_dec(ase_n,f_2)
  138.                        int ase_n, f_2;
  139.      {
  140.        int ec_100, ec_10, ec_1, ase_nn;
  141.        ase_nn = ase_n * ase_n;
  142.        if(f_2 >= ase_nn) ec_100 = f_2 / ase_nn; else ec_100 = 0;
  143.        if(f_2 >= ase_n)
  144.                          {
  145.                             ec_10 = f_2 - ec_100 * ase_nn;
  146.                             ec_10 = ec_10 / ase_n;
  147.                           }
  148.                       else ec_10 = 0;
  149.        if(f_2 >= ase_nn)
  150.               {
  151.                 ec_1 = f_2 - ec_100 * ase_nn - ec_10 * ase_n;
  152.                 if(((ec_100 * ase_nn) % ase_nn) >= (ase_nn/2 -1)) --ec_1;
  153.                 if(((ec_10 * ase_n) % ase_n) >= (ase_n / 2 - 1 )) --ec_1;
  154.               }
  155.        if((f_2 < ase_nn)&(f_2 >= ase_n))
  156.               {
  157.                 ec_1 = f_2 - ec_10 * ase_n;
  158.                 if(((ec_10 * ase_n) % ase_n) >= (ase_n / 2 - 1 )) --ec_1;
  159.               }
  160.        if(f_2 < ase_n) ec_1 = f_2;
  161.        ec_100 = ec_100 + 48; ec_10 = ec_10 + 48;  ec_1 = ec_1 + 48;
  162.        if(ec_100 > 48) prnchar(ec_100);
  163.        if(((ec_100 > 48)&(ec_10 >= 48))|(ec_10 > 48)) prnchar(ec_10);
  164.        prnchar(ec_1);
  165.      }
  166.  
  167. prn_hex(f_2h)
  168.                 int f_2h;
  169.         {
  170.           int ex_16, ex_1;
  171.           if(f_2h >= 16)
  172.                          {
  173.                            ex_16 = f_2h / 16; ex_1 = f_2h - ex_16 * 16;
  174.                            if(((ex_16 * 16) % 16) >= 8) --ex_1;
  175.                          }
  176.                     else { ex_16 = 0; ex_1 = f_2h; }
  177.           if(ex_16 > 9) ex_16 = ex_16 + 7;
  178.           if(ex_1 > 9) ex_1 = ex_1 + 7;
  179.           ex_16 = ex_16 + 48; ex_1 = ex_1 + 48;
  180.           if(ex_16 > 48) prnchar(ex_16);  prnchar(ex_1);
  181.         }
  182.  
  183. prn_bin(zf_2)
  184.                int zf_2;
  185.           {
  186.             int in_8,in_7,in_6,in_5,in_4,in_3,in_2,in_1,in_mod;
  187.             in_8 =in_7 =in_6 =in_5 =in_4 =in_3 =in_2 =in_1 =0;
  188.             in_mod = zf_2 / 128;  if(in_mod > 0) in_8 = 1;
  189.             in_mod = zf_2 / 64; in_mod = in_mod % 2;
  190.             if(in_mod > 0) in_7 = 1;
  191.             in_mod = zf_2 / 32; in_mod = in_mod % 2;
  192.             if(in_mod > 0) in_6 = 1;
  193.             in_mod = zf_2 / 16; in_mod = in_mod % 2;
  194.             if(in_mod > 0) in_5 = 1;
  195.             in_mod = zf_2 / 8; in_mod = in_mod % 2;
  196.             if(in_mod > 0) in_4 = 1;
  197.             in_mod = zf_2 / 4; in_mod = in_mod % 2;
  198.             if(in_mod > 0) in_3 = 1;
  199.             in_mod = zf_2 / 2; in_mod = in_mod % 2;
  200.             if(in_mod > 0) in_2 = 1;
  201.             in_mod = zf_2 % 2; if(in_mod > 0) in_1 = 1;
  202.             if(zf_2 > 255) in_8 = 40;
  203.             prnchar(in_8 + 48); prnchar(in_7 + 48); prnchar(in_6 + 48);
  204.             prnchar(in_5 + 48); prnchar(in_4 + 48); prnchar(in_3 + 48);
  205.             prnchar(in_2 + 48); prnchar(in_1 + 48);
  206.           }
  207.  
  208. prn_str(tr_nm)
  209.                  char tr_nm[];
  210.           {
  211.             char hr_str,tr_end; int tr_lcv; tr_end='a'; tr_lcv = 0;
  212.             while(tr_end != 0)
  213.                   {
  214.                     tr_end = tr_nm[tr_lcv];
  215.                       if(tr_end == '\') {++tr_lcv; prnchar(13);}
  216.                     else {hr_str = tr_nm[tr_lcv]; prnchar(hr_str);}
  217.                     ++tr_lcv;
  218.                   }
  219.           }
  220.  
  221.  
  222.